﻿using Microsoft.AspNetCore.Mvc;
using SMARTHOTEL.RoomTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SMARTHOTEL.Products
{
    /// <summary>
    /// 商品
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class ProductService : ApplicationService, IProductService
    {
        private readonly IRepository<Sort> repository;
        private readonly IRepository<ProductLabel> repository1;
        private readonly IRepository<Product> repository2;
        private readonly IRepository<Reservation> repository3;

        public ProductService(IRepository<Sort> repository, IRepository<ProductLabel> repository1, IRepository<Product> repository2, IRepository<Reservation> repository3)
        {
            this.repository = repository;
            this.repository1 = repository1;
            this.repository2 = repository2;
            this.repository3 = repository3;
        }
        #region 分类
        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateAllAsync(CreateUpdateSortDto dto)
        {
            try
            {
                Sort sort = ObjectMapper.Map<CreateUpdateSortDto, Sort>(dto);
                await repository.InsertAsync(sort);
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败" };
            }
        }
        /// <summary>
        /// 查询分类
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<SortDto>>> GetAllAsync()
        {

            var sort = await repository.GetListAsync();


            return new ResultDto<List<SortDto>>
            {
                Data = ObjectMapper.Map<List<Sort>, List<SortDto>>(sort)
                ,
                Code = StatusCode.Success,
                Message = "查询成功"
            };
        }
        #endregion
        #region 商品标签
        /// <summary>
        /// 新增商品标签
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateProductLabelAsync(CreateUpdateProductLabelDto dto) 
        {
            try
            {
                ProductLabel label = ObjectMapper.Map<CreateUpdateProductLabelDto, ProductLabel>(dto);
                await repository1.InsertAsync(label);
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
            }
            catch (Exception ex)
            {

                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败" };
            }
        }
        /// <summary>
        /// 查询商品标签
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<GetProductLabelDto>>> GetProductLabel()
        {
            var label = await repository1.GetListAsync();
            return new ResultDto<List<GetProductLabelDto>>
            {
                Data = ObjectMapper.Map<List<ProductLabel>, List<GetProductLabelDto>>(label),
                Code = StatusCode.Success,
                Message = "查询成功"
            };
        }
        #endregion
          #region 商品信息
        /// <summary>
        /// 新增商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateAllProduct(CreateProductDto dto)
        {
            try
            {
            Product product = ObjectMapper.Map<CreateProductDto, Product>(dto);
            await repository2.InsertAsync(product);
            return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
            }
            catch (Exception ex)
            {

                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败"+ex.Message };
            }

        }
        /// <summary>
        ///  查询商品
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<GetProductDto>>> GetAllProduct(string? productname, int commoditystatus)
        {
            var product = await repository2.GetListAsync();
            if (productname != null)
            {
                product = product.Where(x => x.ProductName.Contains(productname)).ToList();
            }
            if (commoditystatus != 0)
            {
                product = product.Where(x => x.CommodityStatus == commoditystatus).ToList();
            }
            else
            {
                product = product.ToList();
            }
            return new ResultDto<List<GetProductDto>>
            {
                Data = ObjectMapper.Map<List<Product>, List<GetProductDto>>(product),
                Code = StatusCode.Success,
                Message = "查询成功"
            };
        }
        /// <summary>
        /// 编辑商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> UpdateAllProductAsync(UpdateProductDto dto)
        {
            try
            {
                var product = await repository2.FirstOrDefaultAsync(x => x.Id == dto.Id);
                var entity = ObjectMapper.Map<UpdateProductDto, Product>(dto, product);
                await repository2.UpdateAsync(entity);
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "更新成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "更新失败" };
            }
        }
        /// <summary> 
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteAllProductAsync(string id)
        {
            try
            {
                int i = 0;
                var ids = id.Split(',');
                foreach (var item in ids)
                {
                    var entity = await repository2.GetAsync(x => x.Id == Guid.Parse(item));
                    if (entity == null)
                    {
                        continue;
                    }
                    else
                    {
                        await repository2.DeleteAsync(entity);
                    }
                    i++;
                }
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "删除成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除失败" };
            }
        }
        /// <summary>
        /// 更新商品状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> UpdateProductStatuesAsync(string id)
        {
            try
            {
                int i = 0;
                var ids = id.Split(',');
                foreach (var item in ids)
                {
                    var entity = await repository2.GetAsync(x => x.Id == Guid.Parse(item));
                    if (entity == null)
                    {
                        continue;
                    }
                    else
                    {
                        entity.CommodityStatus = 1;
                        await repository2.UpdateAsync(entity);
                    }
                    i++;
                }
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "更新成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "更新失败" };
            }
        }
        #endregion
        #region 预约信息
        /// <summary>
        /// 新增预约信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateAllReservation(CreateUpdateReservationDto dto)
        {
            try
            {
                var entity = ObjectMapper.Map<CreateUpdateReservationDto, Reservation>(dto);
                await repository3.InsertAsync(entity);
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败" + ex.Message };

            }
        }
        /// <summary>
        /// 查询预约信息
        /// </summary>
        /// <param name="productname"></param>
        /// <param name="commoditystatus"></param>
        /// <returns></returns>
        public async Task<ResultDto<List<GetReservationDto>>> GetAllReservation(string? productname,int commoditystatus)
        {
            var reservation = await repository3.GetListAsync(x=>!x.IsDeleted);
            if (productname != null)
            {
                reservation = reservation.Where(x => x.ReservationName.Contains(productname)).ToList();
            }
            if (commoditystatus != 0)
            {
                reservation = reservation.Where(x => x.CommodityStatus == commoditystatus).ToList();
            }
            else
            {
                reservation = reservation.ToList();
            }
            return new ResultDto<List<GetReservationDto>>
            {
                Data = ObjectMapper.Map<List<Reservation>, List<GetReservationDto>>(reservation),
                Code = StatusCode.Success,
                Message = "查询成功"
            };
        }
        /// <summary> 
        /// 删除预约
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteAllReservationAsync(string id)
        {
            try
            {
                int i = 0;
                var ids = id.Split(',');
                foreach (var item in ids)
                {
                    var entity = await repository3.GetAsync(x => x.Id == Guid.Parse(item));
                    if (entity == null)
                    {
                        continue;
                    }
                    else
                    {
                        await repository3.DeleteAsync(entity);
                    }
                    i++;
                }
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "删除成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除失败" };
            }
        }
        /// <summary>
        /// 更新商品状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> UpdateReservationStatuesAsync(string id)
        {
            try
            {
                int i = 0;
                var ids = id.Split(',');
                foreach (var item in ids)
                {
                    var entity = await repository3.GetAsync(x => x.Id == Guid.Parse(item));
                    if (entity == null)
                    {
                        continue;
                    }
                    else
                    {
                        entity.CommodityStatus = 1;
                        await repository3.UpdateAsync(entity);
                    }
                    i++;
                }
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "更新成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "更新失败" };
            }
        }
        #endregion
    }
}
